Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.upload =
    exports.updateUserInfo =
    exports.loginOut =
    exports.login =
    exports.getUserInfo =
    exports.getUserId =
    exports.getUId =
    exports.getSKEY =
    exports.getLoginStatus =
    exports.getLoginInfo =
      void 0);
var e,
  n =
    (e = require("../@babel/runtime/regenerator")) && e.__esModule
      ? e
      : { default: e },
  t = require("../@babel/runtime/helpers/objectSpread2"),
  r = require("../@babel/runtime/helpers/asyncToGenerator"),
  a = require("./base/user");
var o = (exports.getUserId = function (e) {
    return new Promise(function (o, s) {
      var u,
        i = wx.getStorageSync("san_main_login_info");
      null != i && i.openid && !e
        ? o(i.openid)
        : wx.login({
            provider: "weixin",
            success:
              ((u = r(
                n.default.mark(function e(r) {
                  var s, u, i, c, p;
                  return n.default.wrap(function (e) {
                    for (;;)
                      switch ((e.prev = e.next)) {
                        case 0:
                          if (!(s = r.code)) {
                            e.next = 13;
                            break;
                          }
                          return (
                            (e.next = 4),
                            (0, a.postLogin)({
                              appId: wx.getAccountInfoSync().miniProgram.appId,
                              code: s,
                              thirdType: "WX_APP",
                            })
                          );
                        case 4:
                          if (
                            ((i = e.sent),
                            console.log("获取登录信息", i),
                            200 !== i.code)
                          ) {
                            e.next = 12;
                            break;
                          }
                          return (
                            wx.setStorageSync(
                              "san_main_login_info",
                              t({}, i.data),
                            ),
                            (e.next = 10),
                            (0, a.getAuthorize)({
                              appid: "28477f25d24b4f089f14c8b3a5d868c2",
                              responseType: "code",
                            })
                          );
                        case 10:
                          (p = e.sent),
                            wx.setStorageSync(
                              "san_main_auth_code",
                              null == p || null === (c = p.data) || void 0 === c
                                ? void 0
                                : c.code,
                            );
                        case 12:
                          o(
                            null == i || null === (u = i.data) || void 0 === u
                              ? void 0
                              : u.openid,
                          );
                        case 13:
                        case "end":
                          return e.stop();
                      }
                  }, e);
                }),
              )),
              function (e) {
                return u.apply(this, arguments);
              }),
            fail: function () {
              console.log("登录失败"), s("");
            },
          });
    });
  }),
  s =
    ((exports.getSKEY = (function () {
      var e = r(
        n.default.mark(function e() {
          var t, r, a;
          return n.default.wrap(function (e) {
            for (;;)
              switch ((e.prev = e.next)) {
                case 0:
                  if (
                    ((t = wx.getStorageSync("san_main_login_info")),
                    console.log("info", t),
                    (r = Number(t.loginTime) + t.expiresIn),
                    t && !(r < new Date().getTime()))
                  ) {
                    e.next = 7;
                    break;
                  }
                  return (e.next = 6), o(!0);
                case 6:
                  return e.abrupt(
                    "return",
                    null === (a = wx.getStorageSync("san_main_login_info")) ||
                      void 0 === a
                      ? void 0
                      : a.skey,
                  );
                case 7:
                  return e.abrupt("return", null == t ? void 0 : t.skey);
                case 8:
                case "end":
                  return e.stop();
              }
          }, e);
        }),
      );
      return function () {
        return e.apply(this, arguments);
      };
    })()),
    (exports.getLoginInfo = function () {
      return wx.getStorageSync("san_main_login_info");
    }),
    (exports.getUId = function () {
      var e;
      return (
        (null === (e = wx.getStorageSync("san_main_login_info")) || void 0 === e
          ? void 0
          : e.uid) || ""
      );
    }),
    (exports.upload = function (e) {
      return new Promise(
        (function () {
          var t = r(
            n.default.mark(function t(o) {
              var s;
              return n.default.wrap(function (t) {
                for (;;)
                  switch ((t.prev = t.next)) {
                    case 0:
                      return (t.next = 2), (0, a.getCosAuth)();
                    case 2:
                      (s = t.sent),
                        wx.getFileSystemManager().readFile({
                          filePath: e,
                          success: function (e) {
                            return r(
                              n.default.mark(function t() {
                                var r;
                                return n.default.wrap(function (n) {
                                  for (;;)
                                    switch ((n.prev = n.next)) {
                                      case 0:
                                        return (
                                          (n.next = 2),
                                          (0, a.putUpload)(s.data, e.data)
                                        );
                                      case 2:
                                        (r = n.sent),
                                          console.log("上传成功", s, e, r),
                                          o((s.data || "").split("?")[0]);
                                      case 5:
                                      case "end":
                                        return n.stop();
                                    }
                                }, t);
                              }),
                            )();
                          },
                          fail: function (e) {
                            console.log("上传失败", e), o("");
                          },
                        });
                    case 4:
                    case "end":
                      return t.stop();
                  }
              }, t);
            }),
          );
          return function (e) {
            return t.apply(this, arguments);
          };
        })(),
      );
    }),
    (exports.getUserInfo = function () {
      return wx.getStorageSync("san_main_user_info") || {};
    }));
(exports.updateUserInfo = (function () {
  var e = r(
    n.default.mark(function e(r) {
      var o;
      return n.default.wrap(function (e) {
        for (;;)
          switch ((e.prev = e.next)) {
            case 0:
              return (e.next = 2), (0, a.getUser)();
            case 2:
              return (o = e.sent), (e.next = 5), (0, a.postUpdateUser)(r);
            case 5:
              wx.setStorageSync("san_main_user_info", t(t({}, o.data), r));
            case 6:
            case "end":
              return e.stop();
          }
      }, e);
    }),
  );
  return function (n) {
    return e.apply(this, arguments);
  };
})()),
  (exports.login = (function () {
    var e = r(
      n.default.mark(function e(r) {
        var o, s;
        return n.default.wrap(
          function (e) {
            for (;;)
              switch ((e.prev = e.next)) {
                case 0:
                  return (e.prev = 0), (e.next = 3), (0, a.getUser)();
                case 3:
                  if (((o = e.sent), (s = o.data || {}), !r)) {
                    e.next = 8;
                    break;
                  }
                  return (
                    (e.next = 8),
                    (0, a.postUpdateUser)({
                      avatar: r,
                      nick: s.nick || "微信用户",
                    })
                  );
                case 8:
                  return (
                    wx.setStorageSync(
                      "san_main_user_info",
                      t(
                        t({}, o.data),
                        {},
                        { avatar: r || s.avatar, nick: s.nick || "微信用户" },
                      ),
                    ),
                    e.abrupt("return", !0)
                  );
                case 12:
                  return (
                    (e.prev = 12), (e.t0 = e.catch(0)), e.abrupt("return", !1)
                  );
                case 15:
                case "end":
                  return e.stop();
              }
          },
          e,
          null,
          [[0, 12]],
        );
      }),
    );
    return function (n) {
      return e.apply(this, arguments);
    };
  })()),
  (exports.loginOut = function () {
    wx.setStorageSync("san_main_user_info", {});
  }),
  (exports.getLoginStatus = function () {
    var e;
    return !(null === (e = s()) || void 0 === e || !e.nick);
  });
